package com.wfm.leetcode.editor.cn;

import java.util.ArrayList;
import java.util.List;

/**
 * 汇总区间
 * 2025-03-03 16:50:57
 * 遍历一遍数组，如果后一个数比前一个数大1，就归为一个区间
 * 时间 O(n)
 * 空间复杂度 O(1)
 */
class SummaryRanges {

    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> ret = new ArrayList<String>();
        int i = 0;
        int n = nums.length;
        while (i < n) {
            int low = i;
            i++;
            while (i < n && nums[i] == nums[i - 1] + 1) {
                i++;
            }
            int high = i - 1;
            StringBuffer temp = new StringBuffer(Integer.toString(nums[low]));
            if (low < high) {
                temp.append("->");
                temp.append(nums[high]);
            }
            ret.add(temp.toString());
        }
        return ret;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

    public static void main(String[] args) {
        Solution solution = new SummaryRanges().new Solution();
        
    }
}